﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using Northwind.NET.FE.Reports;
 
namespace Northwind.NET.FE.Forms
{
    public partial class SalesReportsDialog : Form
    {
        private enum SalesReportType
        {
            EmployeeSalesByCountry = 1,
            SalesTotalsByAmount = 2,
            SalesByCategory = 3
        }

        public SalesReportsDialog()
        {
            InitializeComponent();
        }

        private bool _cancelButtonClicked = false;
        private void cancelButton_Click(object sender, EventArgs e)
        {
            _cancelButtonClicked = true;
            this.Close(); 
        }

        private void SalesReportsDialog_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'dataModelDataSet.Category' table. You can move, or remove it, as needed.
            this.categoryTableAdapter.Fill(this.dataModelDataSet.Category);

            categoriesListBox.SelectedItem = null;
            _reportType = SalesReportType.EmployeeSalesByCountry;  
        }

        private SalesReportType _reportType;

        private void toggleControls()
        {
            categoriesListBox.Enabled = salesByCategoryRadioButton.Checked;  
        }
        private void salesByCategoryRadioButton_CheckedChanged(object sender, EventArgs e)
        {
            if (salesByCategoryRadioButton.Checked)
                _reportType = (SalesReportType)Int32.Parse(salesByCategoryRadioButton.Tag.ToString() ); 
            toggleControls();
        }

        private void salesTotalsByAmountRadioButton_CheckedChanged(object sender, EventArgs e)
        {
            if (salesTotalsByAmountRadioButton.Checked)
                _reportType = (SalesReportType)Int32.Parse(salesTotalsByAmountRadioButton.Tag.ToString()) ;
            toggleControls();
        }

        private void employeeSalesByCountryRadioButton_CheckedChanged(object sender, EventArgs e)
        {
            if (employeeSalesByCountryRadioButton.Checked)
                _reportType = (SalesReportType)Int32.Parse(employeeSalesByCountryRadioButton.Tag.ToString()) ;
            toggleControls();
        }

        private int? selectedCategoryId
        {
            get
            {
                return (int?)categoriesListBox.SelectedValue;
            }
        }
        private string selectedCategoryName
        {
            get
            {
                return categoriesListBox.Text;
            }
        }
        //private Form _reportForm;
        private void previewButton_Click(object sender, EventArgs e)
        {
            if (_reportType == SalesReportType.EmployeeSalesByCountry)
            {
                Northwind.NET.FE.Globals.Navigator.OpenEmployeeSalesByCountryReport();
                //this.Close();
            }
            if (_reportType == SalesReportType.SalesTotalsByAmount)
            {
                Northwind.NET.FE.Globals.Navigator.OpenSalesTotalsByAmountReport();
                //this.Close(); 
            } 
            
            if (_reportType == SalesReportType.SalesByCategory)
            {
                if (categoriesListBox.SelectedValue == null)
                {
                    Northwind.NET.FE.Globals.Navigator.OpenSalesByCategoryReport();
                    //this.Close();
                }
                else
                {
                    Northwind.NET.FE.Globals.Navigator.OpenSalesByCategoryReport(categoriesListBox.SelectedValue.ToString() );
                    //this.Close();
                }
            }

        }

        private void SalesReportsDialog_FormClosed(object sender, FormClosedEventArgs e)
        {
            //if (_cancelButtonClicked)
            {
                Northwind.NET.FE.Globals.Navigator.UnhideMainSwitchboard();
            }
        }
    }
}
